using Microsoft.AspNetCore.Mvc;
using QQZiFramework.Mvc;
using QQZiFramework;
using QQZiCore.Mvc;
using QQZiFramework.Membership;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace QQZiCore.SH3H.Api.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class TestController : QQZiCore.Mvc.Controllers.BaseController

    {
        [LoginCheckFilter(IsCheck = true)]
        [HttpGet]
        public string UserInfo()
        {using (DBContext context = CreateDBContext())
            {
           return JM(context.LoginUser);
            }
        }

        [HttpGet]
        public string testMqtt(int monType, [FromHeader] long time, [FromHeader] string token)
        {
            if (time == 0) { throw new System.ArgumentNullException("time"); }
            if (token == null) { throw new System.ArgumentNullException("token"); }
            return time + "..." + token;
        }

            [HttpPost]
        public string Login(int userId)
        {
            if (!IsDevelopment()) throw new NotAlowAccessException();
            JsonMessage jm = new JsonMessage();
            using (DBContext context = CreateDBContext())
            {
                TokenInfo tokenInfo;
                User u = null;
                if (context.LoginUser != null && context.LoginUser.UserId == userId)
                {
                    tokenInfo = context.Membership.GetTokenInfo(context);
                    u = (User)context.LoginUser;
                }
                else
                {
                    // context.GetBusiness<MemberShip>().ClearUserInCache(userId);
                    u = new User() { Name = "test", UserId = 1, Mobile = "17097913883" };
                }
                jm.Result = OnUserLogin(u, null);
            }
            return jm.ToJson();
        }

        private object OnUserLogin(User u, string openId)
        {
            DBContext context = CreateDBContext();


            TokenInfo tokenInfo = RecordLogin(u, LoginUserStateType.Redis, LoginPlatfromId.App, null, context);

            bool ShowVipBtn = context.GetSettingValue<bool>("WechatApp-ShowVip", false, false);
            return new { WXMiniOpenId = openId, platform = 1, id = u.UserId, account = u.Account, name = u.Name, nickName = u.NickName, headPicture = u.HeadPicture, mobile = u.Mobile, token = tokenInfo.Token, secret = tokenInfo.AppSecret, expiresAt = tokenInfo.ExpiresAt, ShowVipBtn = ShowVipBtn };

        }

          [HttpGet]
        public string SettingTest(){
            using(DBContext context = CreateDBContext()){
                InfluxDB_Parameter param= context.GetSettingValue<InfluxDB_Parameter>("InfluxDB");
                return GetAppSetting<string>("InfluxDB:Api",'\\',true);
            }
        }

           /// <summary>
        /// influxDB参数获取
        /// </summary>
        public class InfluxDB_Parameter
        {
            public string api { get; set; }

            public string user { get; set; }

            public string password { get; set; }

            public string dbName { get; set; }

            public string measurement { get; set; }
        }


    }
}